package com.hoshiicloud.fileserver.util;

import com.hoshiicloud.fileserver.config.ObsConfig;
import com.obs.services.ObsClient;
import com.obs.services.model.ListBucketsRequest;
import com.obs.services.model.ObsBucket;
import com.obs.services.model.StorageClassEnum;

import java.util.List;

public class ObsUtil {

	/**
	 * 判断存储空间是否存在
	 * 
	 * @param bucketName
	 * @return
	 */
	public static boolean bucketIsExists(String bucketName) throws Exception{

		List<ObsBucket> list = getObsBucketList();
		for (ObsBucket obsBucket : list) {
			if (obsBucket.getBucketName().equals(bucketName)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 查询所有区域的存储空间
	 * 
	 * @return
	 */
	public static List<ObsBucket> getObsBucketList() throws Exception {
		ObsClient obsClient = ObsConfig.getDefaultObsClient();
		ListBucketsRequest request = new ListBucketsRequest();
		// 设置setQueryLocation参数为true后，可在列举存储空间时查询存储空间的区域位置
		request.setQueryLocation(true);
		List<ObsBucket> list=obsClient.listBuckets(request);
		obsClient.close();
		return list;
	}

	public static StorageClassEnum getStorageTypeEnum() {
		// TODO 根据storageType匹配返回,默认为标准存储模式
		return StorageClassEnum.STANDARD;
	}

}
